/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingWorker;

/**
 *
 * @author bob
 */
public class Maintenance extends javax.swing.JFrame {

    /**
     * Creates new form Maintenance
     */
    PopUpTable pt;
    boolean editing = true;        
    public Maintenance() throws SQLException {
        initComponents();
        //showAll();
    }

    private void showAll() throws SQLException
    {
        SwingWorker bgThread = new SwingWorker()
        {
            ResultSet rset;

            @Override
            protected Object doInBackground() throws Exception 
            {
                pt = new PopUpTable("SELECT * FROM MAINTENANCETICKET WHERE COMPLETEDDATE IS NULL ");
                
                        throw new UnsupportedOperationException("Not supported yet.");
            }
            
            @Override
            protected void done()
            {
                pt.setVisible(true);                
                        
            }
           
        };
        bgThread.execute();
        
   }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        backButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        descriptionBox = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        maintenanceSubmitButton = new javax.swing.JButton();
        showButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        eSSNBox = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Maintenance", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14), new java.awt.Color(0, 0, 0))); // NOI18N
        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jPanel1MouseEntered(evt);
            }
        });

        backButton.setText("Back");
        backButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                backButtonMouseClicked(evt);
            }
        });
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        descriptionBox.setColumns(20);
        descriptionBox.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
        descriptionBox.setLineWrap(true);
        descriptionBox.setRows(5);
        jScrollPane2.setViewportView(descriptionBox);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Description:");

        maintenanceSubmitButton.setText("Submit");
        maintenanceSubmitButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                maintenanceSubmitButtonMouseClicked(evt);
            }
        });
        maintenanceSubmitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maintenanceSubmitButtonActionPerformed(evt);
            }
        });

        showButton.setText("Show Tickets");
        showButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                showButtonMouseClicked(evt);
            }
        });

        jLabel1.setText("Employee SSN:");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(0, 0, Short.MAX_VALUE)
                        .add(showButton))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jLabel2)
                            .add(jLabel1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(jPanel1Layout.createSequentialGroup()
                                    .add(maintenanceSubmitButton)
                                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 172, Short.MAX_VALUE)
                                    .add(backButton))
                                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 267, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(eSSNBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 102, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(165, 165, 165))))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(showButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(eSSNBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 179, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(maintenanceSubmitButton)
                    .add(backButton)))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void maintenanceSubmitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maintenanceSubmitButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_maintenanceSubmitButtonActionPerformed

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_backButtonActionPerformed

    private void backButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_backButtonMouseClicked
        //create main window
        MainMenu main = new MainMenu();
        main.setLocation(MainMenu.X, MainMenu.Y);
        main.setVisible(true);

        //close this window
        this.dispose();
    }//GEN-LAST:event_backButtonMouseClicked

    private void showButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_showButtonMouseClicked
        try {   
            showAll();
            editing = false;
        } catch (SQLException ex) {
            Logger.getLogger(Maintenance.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_showButtonMouseClicked

    private void jPanel1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MouseEntered
        if(!editing)
        {
            String[] text = pt.stringFromRow();
            if(text.length >1 )
            {
                System.out.println(text.length);        
                descriptionBox.setText(text[4]);
                //dateBox.setText(text[3]);              
                
                editing = true;
            }// TODO add your handling code here:
        }
    }//GEN-LAST:event_jPanel1MouseEntered

    private void maintenanceSubmitButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_maintenanceSubmitButtonMouseClicked
        String[] text = pt.stringFromRow();  
        String ssn = eSSNBox.getText();
        try {
                String query = "UPDATE MAINTENANCETICKET "
                        + "SET COMPLETEDDATE = NOW() "
                        + "WHERE ISSUE = ? AND INITIALDATE = ? ";
                        
                DBconnection db = new DBconnection();
                Connection conn = db.getConnection();
                PreparedStatement statement = null;
                statement = conn.prepareStatement(query);                            
                ///////////////////////////////
              
                statement.setString(1, text[1] );
                statement.setString(2, text[2]);        
             
                
                ///////////////////////////////
                statement.executeUpdate();
                
                query = "INSERT INTO WORKSON VALUES(?,?,?,?)";
                statement = conn.prepareStatement(query);
                statement.setString(1, ssn);  
                statement.setString(2, text[1]);  
                statement.setString(3, text[2]);  
                statement.setString(4, text[0]);  
                
                statement.close();
                conn.close();
                
                descriptionBox.setText(null);
            } catch (SQLException ex) {
                Logger.getLogger(Discipline.class.getName()).log(Level.SEVERE, null, ex);
            }
    }//GEN-LAST:event_maintenanceSubmitButtonMouseClicked

    /**
     * @param args the command line arguments
     */
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton;
    private javax.swing.JTextArea descriptionBox;
    private javax.swing.JTextField eSSNBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton maintenanceSubmitButton;
    private javax.swing.JButton showButton;
    // End of variables declaration//GEN-END:variables
}
